class Rational(n: Int, d: Int) {
  require(d != 0)

  private val g = gcd(n.abs, d.abs)
  val number: Int = n / g;
  val denom: Int = d / g;

  override def toString: String = number + "/" + denom;

  def add(that: Rational): Rational =
    new Rational(number * that.denom + that.number * this.denom, this.denom * that.denom)

  def this(n: Int) = this(n, 1)

  private def gcd(a: Int, b: Int): Int = {
    if (b == 0) a else gcd(b, a % b)
  }

  def + (that: Rational) : Rational = {
    new Rational(number * that.denom + that.number * this.denom, this.denom * that.denom)
  }
}

object TMain {
  def main(args: Array[String]): Unit = {


    // println(new Rational(5, 0))
    val oneHalf = new Rational(1, 2)
    val twoThirds = new Rational(1, 2)
    println (oneHalf add twoThirds)

//    val three = new Rational(3)
//    println (three)
  }
}